package org.chorus;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.gicentre.utils.gui.Tooltip;
import org.gicentre.utils.spatial.Direction;

import processing.core.PApplet;

//  *********************************************************************************************
/** Class for displaying a timed message on screen.
 *  @author Jo Wood, giCentre.
 *  @version 1.1, 13th March, 2013.
 */ 
//  *********************************************************************************************

/* This file is part of the Chorus river bed analysis software package. Chorus is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * Chorus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this
 * source code (see COPYING included with this source code). If not, see 
 * http://www.gnu.org/licenses/.
 */
public class ScreenMessager 
{
	// ------------------------ Object and class variables --------------------------

	private ArrayList<Tooltip> tooltips;	// Used to displaying the messages.
	private PApplet parent;					// Sketch in which this message will be displayed.
	private boolean doDrawing;				// Determines if message is to be displayed.
	
	// ------------------------------ Constructors ----------------------------------
	
	/** Creates a new messager capable of displaying simple screen messages for a fixed 
	 *  length of time.
	 */
	public ScreenMessager(PApplet parent)
	{
		this.parent = parent;
		tooltips = new ArrayList<Tooltip>();
	}
	
	// --------------------------------- Methods ------------------------------------
	
	/** Displays the given text for the given length of time.
	 *  @param message Message to display.
	 *  @param numSeconds Length of time to display the message in seconds.
	 */
	public void displayText(String message, int numSeconds)
	{
		Tooltip tooltip = new Tooltip(parent,DisplayUtils.mediumFont,18,320);
		tooltip.setBackgroundColour(parent.color(0,200));
		tooltip.setTextColour(parent.color(255,200));
		tooltip.setAnchor(Direction.NORTH);
		tooltip.setIsCurved(true);
		tooltips.add(tooltip);
		
		tooltip.setText(message);
		tooltip.setIsActive(true);
		
		Timer timer = new Timer();
		doDrawing = true;
		parent.loop();
		timer.schedule(new UpdateTask(tooltip),numSeconds*1000);
	}
	
	/** Displays the message on screen.
	 *  @param xOrigin Left hand side position of display area in pixel units.
	 *  @param yOrigin Top position of display area in pixel units.
	 *  @param width Width of display area in pixel units.
	 *  @param height Height of display area in pixel units.
	 */
	public void draw(float xOrigin, float yOrigin, float width, float height)
	{
		if (doDrawing == false)
		{
			return;
		}
		
		float tipHeightTotal = 0;
		for (Tooltip tooltip: tooltips)
		{
			tooltip.draw(xOrigin+width/2,yOrigin+height/2+tipHeightTotal);
			tipHeightTotal += tooltip.getHeight()+10;
		}
	}
	
	/** Reports whether or not a message should be displayed.
	 *  @return true True if a message should be displayed.
	 */
	public boolean isMessageDisplayed()
	{
		return doDrawing;
	}
	
	
	/** Update task that updates the display after a fixed interval.
	  */
	private class UpdateTask extends TimerTask
	{
		private Tooltip tooltip;		// The tooltip associated with this timed event.
		
		/** Creates the new update task which will stop drawing after a fixed time.
		 */
		public UpdateTask(Tooltip tooltip) 
		{
			this.tooltip = tooltip;
		}

		/** Tells the messager to stop displaying.
		 */
		@SuppressWarnings("synthetic-access")
		public void run() 
		{
			tooltip.setIsActive(false);
			tooltips.remove(tooltip);
			
			if (tooltips.size() == 0)
			{
				doDrawing = false;
			}
			
			parent.loop();
		}
	}
}
